﻿using ERPSystem.Common.Domains.PurchaseInvoiceCommon;
using ERPSystem.Common.DTO;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;
using SqlSugar;

namespace SalesManagementCommon.API.SalesManagementCommons.PurchaseInvoiceRead
{
    public class PurchaseInvoiceReadQueryHandler : IRequestHandler<PurchaseInvoiceReadQueryCommand, Result<PageResultDto<PurchaseInvoiceReadQueryDto>>>
    {
        private readonly ISqlSugarClient _sqlSugarClient;

        public PurchaseInvoiceReadQueryHandler(ISqlSugarClient sqlSugarClient)
        {
            _sqlSugarClient = sqlSugarClient;
        }
        /// <summary>
        /// 采购发票
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<PageResultDto<PurchaseInvoiceReadQueryDto>>> Handle(PurchaseInvoiceReadQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<PageResultDto<PurchaseInvoiceReadQueryDto>>();
            var page = new PageResultDto<PurchaseInvoiceReadQueryDto>() { TotalCount = 0, TotalPage = 0, List = new List<PurchaseInvoiceReadQueryDto>() };
            try
            {
                RefAsync<int> totalCount = 0;
                RefAsync<int> totalPage = 0;
                var list = _sqlSugarClient.Queryable<PurchaseInvoice, PurchaseInvoiceSubtable>((a, b) => a.SerialNumber == b.SerialNumber).Select((a, b) => new PurchaseInvoiceReadQueryDto()
                {
                    PurchaseInvoiceId = a.PurchaseInvoiceId,
                    DocumentNo = a.DocumentNo,
                    BusinessType = a.BusinessType,
                    Vendor = a.Vendor,
                    BillingDate = a.BillingDate,
                    OnInvoice = a.OnInvoice,
                    DocumentDate = a.DocumentDate,
                    Operator = a.Operator,
                    TaxNo = a.TaxNo,
                    RedBasketLetter = a.RedBasketLetter,
                    XMLFormat = a.XMLFormat,
                    DocumentStatus = a.DocumentStatus,
                    Auditors = a.Auditors,
                    InvoiceTypeId = a.InvoiceTypeId,
                    InvoiceAmount = a.InvoiceAmount,
                    OFPFormat = a.OFPFormat,
                    BusinessStatusId = a.BusinessStatusId,
                    DateOfReview = a.DateOfReview,
                    InvoiceCode = a.InvoiceCode,
                    Tax = a.Tax,
                    InvoiceStatus = a.InvoiceStatus,
                    InvoiceNumber = a.InvoiceNumber,
                    PriceTaxTotal = a.PriceTaxTotal,
                    RemarkInfo = a.RemarkInfo,
                    TaxRate= b.TaxRate,
                    AmountOfTax = b.AmountOfTax
                }).MergeTable();
                if (!string.IsNullOrEmpty(request.DocumentNo))
                {
                    list = list.Where(a => a.DocumentNo.Contains(request.DocumentNo));
                }
                if (!string.IsNullOrEmpty(request.Vendor))
                {
                    list = list.Where(a => a.Vendor.Contains(request.Vendor));
                }
                if (!string.IsNullOrEmpty(request.InvoiceNumber))
                {
                    list = list.Where(a => a.InvoiceNumber.Contains(request.InvoiceNumber));
                }
                var res = await list.ToPageListAsync(request.PageIndex, request.PageSize, totalCount, totalPage);
                page.TotalCount = totalCount;
                page.TotalPage = totalPage;
                page.List = res;
                if (page.List.Any())
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = page;
                    result.Msg = "获取采购发票信息成功";
                }

            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
